import request from "@/utils/request.js";
import {desDecrypt, genDesKey, rsaPubEncrypt} from "@/utils/EncryptUtils.js";
import {resolvePromise_data, resolvePromise_status} from "@/utils/ReturnUtils.js";
import {elMessageTip} from "@/utils/MessageTipUtils.js";
import {post} from "@/utils/http.js";
import useAccountStore from "@/store/modules/AccountStore.js";

/**
 * 获取全部的商品分类
 *
 * @returns {Promise<*>}
 */
export const getProductCategoryAllAPI = () => {
    const desKey = genDesKey()
    return request.get(
        '/product/category/all',
        {
            params: {
                desKey: rsaPubEncrypt(desKey)
            }
        }
    ).then(
        res => {
            res = desDecrypt(res, desKey)
            // elMessageTip(res)
            return resolvePromise_data(res)
        }
    )
}

/**
 * 随机获取指定个数商品的商品列表
 *
 * @param productCount
 * @param productCategoryId
 * @param productCategorySubId
 */
export const getProductListRandomAPI = (productCount, productCategoryId = null, productCategorySubId = null) => {
    const desKey = genDesKey()
    return request.get(
        '/product/random',
        {
            params: {
                desKey: rsaPubEncrypt(desKey),
                productCount, productCategoryId, productCategorySubId
            }
        }
    ).then(
        res => {
            res = desDecrypt(res, desKey)
            // elMessageTip(res)
            return resolvePromise_data(res)
        }
    )
};

/**
 * 根据 id 获取商品信息
 *
 * @param productId
 */
export const getProductById = (productId) => {
    const desKey = genDesKey()
    return request.get(
        `/product/${productId}`,
        {
            params: {
                desKey: rsaPubEncrypt(desKey)
            }
        }
    ).then(
        res => {
            res = desDecrypt(res, desKey)
            return resolvePromise_data(res)
        }
    )
}

export const getProductByKw = (kw) => {
    const desKey = genDesKey()
    return request.get(
        `/product/kw`,
        {
            params: {
                desKey: rsaPubEncrypt(desKey),
                kw: rsaPubEncrypt(kw),
            }
        }
    ).then(
        res => {
            res = desDecrypt(res, desKey)
            return resolvePromise_data(res)
        }
    )
}

export const getCommentById = (productId, userId = null) => {
    const desKey = genDesKey()
    return request.get(
        `/product/comment`,
        {
            params: {
                desKey: rsaPubEncrypt(desKey),
                productId, userId
            }
        }
    ).then(
        res => {
            res = desDecrypt(res, desKey)
            return resolvePromise_data(res)
        }
    )
}

export const delCommentById = (commentId) => {
    return post(
        `/product/comment/del`,
        {
            account: rsaPubEncrypt(useAccountStore().account),
            commentId
        }
    ).then(
        res => {
            elMessageTip(res)
            return resolvePromise_status(res)
        }
    )
}